package com.jbpm.service;

import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.file.def.FileDefinition;
import org.jbpm.graph.def.ProcessDefinition;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.Map;

public class GetProcessImageServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);

	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String pdid = request.getParameter("pdid");
		JbpmContext ctx = JbpmConfiguration.getInstance().getCurrentJbpmContext();
		ProcessDefinition pd = ctx.getGraphSession().loadProcessDefinition(Long.parseLong(pdid));
		FileDefinition fd = pd.getFileDefinition();
		Map bytemap = fd.getBytesMap();

		response.setContentType("application/x-zip-compressed");
		for (Iterator iterator = bytemap.keySet().iterator(); iterator.hasNext();) {
			String name = (String)iterator.next();
			if(name.lastIndexOf(".jpg")>-1){
				byte filebyte[] = fd.getBytes(name);
				OutputStream out = response.getOutputStream();
				out.write(filebyte);
				out.flush();
				out.close();
			}

		}

	}


}
